gdkcontentformats: Change the matching API
authorBenjamin Otte <otte@redhat.com>
Fri, 24 Nov 2017 10:34:19 +0000 (11:34 +0100)
committerBenjamin Otte <otte@redhat.com>
Sun, 3 Dec 2017 04:46:47 +0000 (05:46 +0100)
Instead of having just one function that has the gtype and mime type as
out arguments, have 3 functions: 1 that finds any match, 1 that finds a
GType match and one for a mime type match.

This makes the API way more convenient to use.

docs/reference/gdk/gdk4-sections.txt
gdk/gdkclipboard.c
gdk/gdkcontentformats.c
gdk/gdkcontentformats.h
gtk/gtkdragdest.c
gtk/gtkdragdest.h
tests/testdnd.c

index 9097af5db15522cea4c5e6d098b833166a39452e..8143c7d33522447005f4928095f1883ceb372b37 100644 (file)
@@ -377,6 +377,8 @@ gdk_content_formats_get_gtypes
 gdk_content_formats_get_mime_types
 gdk_content_formats_union
 gdk_content_formats_match
+gdk_content_formats_match_gtype
+gdk_content_formats_match_mime_types
 gdk_content_formats_contain_gtype
 gdk_content_formats_contain_mime_type
 
index b5dc5dbd75102712cc0a7aa6ed10534ef4f68c13..38df1c5012928566364aae4cc8b36f2645e6b247 100644 (file)
@@ -166,9 +166,9 @@ gdk_clipboard_read_local_async (GdkClipboard        *clipboard,
 
   content_formats = gdk_content_provider_ref_formats (priv->content);
   content_formats = gdk_content_formats_union_serialize_mime_types (content_formats);
+  mime_type = gdk_content_formats_match_mime_type (content_formats, formats);
 
-  if (gdk_content_formats_match (content_formats, formats, NULL, &mime_type)
-      && mime_type != NULL)
+  if (mime_type != NULL)
     {
       GOutputStream *output_stream;
       GIOStream *stream;
@@ -892,7 +892,8 @@ gdk_clipboard_write_async (GdkClipboard        *clipboard,
 
   mime_formats = gdk_content_formats_new ((const gchar *[2]) { mime_type, NULL }, 1);
   mime_formats = gdk_content_formats_union_serialize_gtypes (mime_formats);
-  if (gdk_content_formats_match (mime_formats, formats, &gtype, NULL))
+  gtype = gdk_content_formats_match_gtype (formats, mime_formats);
+  if (gtype != G_TYPE_INVALID)
     {
       GValue value = G_VALUE_INIT;
       GError *error = NULL;
index 2663a3bbc7bb274b0b296cbacc6659b483a397e0..7b4a101e13a6902b821ac603138267dabe18b4c1 100644 (file)
@@ -276,55 +276,78 @@ gdk_content_formats_contain_interned_mime_type (const GdkContentFormats *formats
  * gdk_content_formats_match:
  * @first: the primary #GdkContentFormats to intersect
  * @second: the #GdkContentFormats to intersect with
- * @out_gtype: (out) (allow-none): pointer to take the 
- *     matching #GType or %G_TYPE_INVALID if @out_mime_type was set.
- * @out_mime_type: (out) (allow-none) (transfer none): The matching
- *    mime type or %NULL if @out_gtype is set
  *
- * Finds the first element from @first that is also contained
- * in @second. If no matching format is found, %FALSE is returned
- * and @out_gtype and @out_mime_type are set to %G_TYPE_INVALID and
- * %NULL respectively.
+ * Checks if @first and @second have any matching formats.
  *
  * Returns: %TRUE if a matching format was found.
  */
 gboolean
 gdk_content_formats_match (const GdkContentFormats *first,
-                           const GdkContentFormats *second,
-                           GType                   *out_gtype,
-                           const char             **out_mime_type)
+                           const GdkContentFormats *second)
+{
+  g_return_val_if_fail (first != NULL, FALSE);
+  g_return_val_if_fail (second != NULL, FALSE);
+
+  return gdk_content_formats_match_gtype (first, second) != G_TYPE_INVALID
+      || gdk_content_formats_match_mime_type (first, second) != NULL;
+}
+
+/**
+ * gdk_content_formats_match_gtype:
+ * @first: the primary #GdkContentFormats to intersect
+ * @second: the #GdkContentFormats to intersect with
+ *
+ * Finds the first #GType from @first that is also contained
+ * in @second. If no matching #GType is found, %G_TYPE_INVALID
+ * is returned.
+ *
+ * Returns: The first common #GType or %G_TYPE_INVALID if none.
+ **/
+GType
+gdk_content_formats_match_gtype (const GdkContentFormats *first,
+                                 const GdkContentFormats *second)
 {
   gsize i;
 
   g_return_val_if_fail (first != NULL, FALSE);
   g_return_val_if_fail (second != NULL, FALSE);
 
-  if (out_gtype)
-    *out_gtype = G_TYPE_INVALID;
-  if (out_mime_type)
-    *out_mime_type = NULL;
-
   for (i = 0; i < first->n_gtypes; i++)
     {
       if (gdk_content_formats_contain_gtype (second, first->gtypes[i]))
-        {
-          if (out_gtype)
-            *out_gtype = first->gtypes[i];
-          return TRUE;
-        }
+        return first->gtypes[i];
     }
 
+  return G_TYPE_INVALID;
+}
+
+/**
+ * gdk_content_formats_match_mime_type:
+ * @first: the primary #GdkContentFormats to intersect
+ * @second: the #GdkContentFormats to intersect with
+ *
+ * Finds the first mime type from @first that is also contained
+ * in @second. If no matching mime type is found, %NULL is
+ * returned.
+ *
+ * Returns: The first common mime type or %NULL if none.
+ **/
+const char *
+gdk_content_formats_match_mime_type (const GdkContentFormats *first,
+                                     const GdkContentFormats *second)
+{
+  gsize i;
+
+  g_return_val_if_fail (first != NULL, FALSE);
+  g_return_val_if_fail (second != NULL, FALSE);
+
   for (i = 0; i < first->n_mime_types; i++)
     {
       if (gdk_content_formats_contain_interned_mime_type (second, first->mime_types[i]))
-        {
-          if (out_mime_type)
-            *out_mime_type = first->mime_types[i];
-          return TRUE;
-        }
+        return first->mime_types[i];
     }
 
-  return FALSE;
+  return NULL;
 }
 
 /**
@@ -534,6 +557,7 @@ gdk_content_formats_builder_add_gtype (GdkContentFormatsBuilder *builder,
                                        GType                     type)
 {
   g_return_if_fail (builder != NULL);
+  g_return_if_fail (type != G_TYPE_INVALID);
 
   if (g_slist_find (builder->gtypes, GSIZE_TO_POINTER (type)))
     return;
index 7ba149631623f8bb6c652086d118fbcea0cfc224..1c67d005f35f78998b5126724fc5720d75ac8259 100644 (file)
@@ -58,9 +58,13 @@ GdkContentFormats *     gdk_content_formats_union               (GdkContentForma
                                                                  const GdkContentFormats        *second) G_GNUC_WARN_UNUSED_RESULT;
 GDK_AVAILABLE_IN_3_94
 gboolean                gdk_content_formats_match               (const GdkContentFormats        *first,
-                                                                 const GdkContentFormats        *second,
-                                                                 GType                          *out_gtype,
-                                                                 const char                    **out_mime_type);
+                                                                 const GdkContentFormats        *second);
+GDK_AVAILABLE_IN_3_94
+GType                   gdk_content_formats_match_gtype         (const GdkContentFormats        *first,
+                                                                 const GdkContentFormats        *second);
+GDK_AVAILABLE_IN_3_94
+const char *            gdk_content_formats_match_mime_type     (const GdkContentFormats        *first,
+                                                                 const GdkContentFormats        *second);
 GDK_AVAILABLE_IN_3_94
 gboolean                gdk_content_formats_contain_gtype       (const GdkContentFormats        *formats,
                                                                  GType                           type);
index 8b9fbff74f9a1cb18ea95f974b1e2bc5291f605a..5c5c09af9cd6842ae705614891f221c2056a3ee6 100644 (file)
@@ -406,13 +406,11 @@ gtk_drag_dest_get_track_motion (GtkWidget *widget)
  * Returns: (transfer none) (nullable): first target that the source offers
  *     and the dest can accept, or %NULL
  */
-GdkAtom
+const char *
 gtk_drag_dest_find_target (GtkWidget         *widget,
                            GdkDragContext    *context,
                            GdkContentFormats *target_list)
 {
-  GdkAtom result;
-
   g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
   g_return_val_if_fail (GDK_IS_DRAG_CONTEXT (context), NULL);
 
@@ -422,11 +420,7 @@ gtk_drag_dest_find_target (GtkWidget         *widget,
   if (target_list == NULL)
     return NULL;
 
-  gdk_content_formats_match (target_list,
-                             gdk_drag_context_get_formats (context),
-                             NULL,
-                             &result);
-
-  return result;
+  return gdk_content_formats_match_mime_type (target_list,
+                                              gdk_drag_context_get_formats (context));
 }
 
index d69bd06eec99b998969df17924f1241b9e47ac58..9fc1d3ee319cbc84c37221415f3b15f12c0577a0 100644 (file)
@@ -76,7 +76,7 @@ GDK_AVAILABLE_IN_ALL
 void gtk_drag_dest_unset (GtkWidget          *widget);
 
 GDK_AVAILABLE_IN_ALL
-GdkAtom                 gtk_drag_dest_find_target       (GtkWidget              *widget,
+const char *            gtk_drag_dest_find_target       (GtkWidget              *widget,
                                                          GdkDragContext         *context,
                                                          GdkContentFormats      *target_list);
 GDK_AVAILABLE_IN_ALL
index 04ec5bf4674556bf770846d607c082bc3ed8b35b..e2f81e463bb4c26fa00652e3e08f8239fecc1597 100644 (file)
@@ -344,7 +344,7 @@ target_drag_drop       (GtkWidget          *widget,
                            guint               time)
 {
   GdkContentFormats *formats;
-  GdkAtom format;
+  const char *format;
 
   g_print("drop\n");
   have_drag = FALSE;
@@ -352,7 +352,7 @@ target_drag_drop       (GtkWidget          *widget,
   gtk_image_set_from_pixbuf (GTK_IMAGE (widget), trashcan_closed);
 
   formats = gdk_drag_context_get_formats (context);
-  gdk_content_formats_match (formats, formats, NULL, &format);
+  format = gdk_content_formats_match_mime_type (formats, formats);
   if (format)
     {
       gtk_drag_get_data (widget, context,